# -*- Mode: Makefile; -*- 

#
# This variable specifies where we are.  Lower-level make files will need an
# absolute path so we use 'pwd' rather than "."
#
CHOMBO_HOME = $(shell pwd)
PYTHON_INCLUDE = /usr/local/include/python2.1

export CHOMBO_HOME

CLASSES := IntVect Box ProblemDomain IntVectSet DataIndex LayoutIterator DataIterator \
BoxLayout DisjointBoxLayout FArrayBox LDF AMRIO Python_Utils

_lib_names := arrayview amrtools boxtools basetools
##
## Default values of configuration variables.
##

include $(CHOMBO_HOME)/mk/Make.defs
include $(CHOMBO_HOME)/mk/Make.defs.config

_cppflags    := -DCH_SPACEDIM=$(DIM) \
  $(subst DOUBLE,-DCH_USE_DOUBLE,$(subst FLOAT,-DCH_USE_FLOAT,$(PRECISION))) \
  $(subst FALSE,-DNDEBUG,$(subst TRUE,,$(DEBUG))) \
  $(subst FALSE,,$(subst TRUE,-DMPI,$(MPI)))
_cxxflags    := $(subst FALSE,$(cxxoptflags),$(subst TRUE,$(cxxdbgflags),$(DEBUG))) \
  $(subst FALSE,,$(subst TRUE,$(cxxprofflags),$(PROFILE)))
_libflags := -L$(CHOMBO_HOME) $(patsubst %,-l%$(config),$(_lib_names)) $(instlibflags) $(mpilibflags) $(flibflags) \
  $(subst FALSE,,$(subst TRUE,$(x11libflags),$(NEEDS_X11))) $(syslibflags)

CPPFLAGS   := $(_cppflags) -DHDF5
CXXFLAGS   := $(_cxxflags)
LIBFLAGS   := $(_libflags)
LDFLAGS     = $(CXXFLAGS) $(sysldflags)
LIBFLAGS   := $(_libflags)

MODULENAME := chombo_$(DIM)d$(subst DOUBLE,,$(subst FLOAT,_FLOAT,$(PRECISION)))$(subst FALSE,,$(subst TRUE,_DEBUG,$(DEBUG)))

HEADERS := $(addprefix include/,$(addsuffix .H, $(CLASSES)))

INTERFACES := $(addprefix $(MODULENAME)/,$(addsuffix .i, $(CLASSES)))


$(MODULENAME)/$(MODULENAME)cmodule.so: $(MODULENAME)/$(MODULENAME).o
	$(LD) $(cxxsharedflag) $(LDFLAGS) $(XTRALDFLAGS) $< $(LIBFLAGS) $(XTRALIBFLAGS) -o $@

$(MODULENAME)/$(MODULENAME).o : $(MODULENAME)/$(MODULENAME).c
	$(cxxc) $(CXXFLAGS) $(XTRACXXFLAGS) $(CPPFLAGS) -Iinclude -I/usr/local/include/python2.1 $(XTRACPPFLAGS) $(cxxcppflags) \
		-DCH_LANG_CC $(cxxcompflag) $< -o $@

$(MODULENAME)/$(MODULENAME).c $(MODULENAME)/$(MODULENAME).py: chombo.i $(HEADERS) $(INTERFACES) $(MODULENAME)
	-swig  -DCHOMBO=$(MODULENAME) -I$(MODULENAME) -python -c++ -shadow -o $(MODULENAME)/$(MODULENAME).c  chombo.i 2>&1 > /dev/null

$(MODULENAME)/%.i: include/%.H $(MODULENAME)
	cp include/$*.H $(MODULENAME)/$*.c
	$(CH_CPP) -Iinclude -Dexplicit= -DWRAPPER $(CPPFLAGS)  $(MODULENAME)/$*.c > $@

$(MODULENAME):
	mkdir -p $(MODULENAME)

clean: 
	rm -rf $(MODULENAME)
